package com.itheima.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class UserServiceImplProxy {
    public static <T> T getProxy(T userService){
       return (T) Proxy.newProxyInstance(userService.getClass().getClassLoader(),
                userService.getClass().getInterfaces(), new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("方法开始");
                        long startTime = System.currentTimeMillis();
                        Object o = method.invoke(userService,args);
                        long endTime = System.currentTimeMillis();
                        System.out.println("方法结束");
                        System.out.println(method.getName()+"方法耗时"+(endTime-startTime)/1000.0 +"s");
                        return  o;
                    }
                });
    }
}
